<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/6
 * Time: 19:37
 */



//昨天时间
$yes = date("Y-m-d",strtotime("-1 day"));
//前一天时间
$yes = date("Y-m-d",(strtotime($day) - 3600*24));


//毫秒时间
function get_microtime()
{
    list($usec, $sec) = explode(' ', microtime());
    return ((float)$usec + (float)$sec);
}

//计算该月有几天
function getdaysInmonth($month, $year)
{
    $days = '';
    if ($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12)
        $days = 31;
    else if ($month == 4 || $month == 6 || $month == 9 || $month == 11)
        $days = 30;
    else if ($month == 2) {
        if (isLeapyear($year)) {
            $days = 29;
        } else {
            $days = 28;
        }
    }
    return ($days);
}

/**
 * 日期转换成几分钟前
 */
function formatTime($date)
{
    $timer = strtotime($date);
    $diff = $_SERVER['REQUEST_TIME'] - $timer;
    $day = floor($diff / 86400);
    $free = $diff % 86400;
    if ($day > 0) {
        if (15 < $day && $day < 30) {
            return "半个月前";
        } elseif (30 <= $day && $day < 90) {
            return "1个月前";
        } elseif (90 <= $day && $day < 187) {
            return "3个月前";
        } elseif (187 <= $day && $day < 365) {
            return "半年前";
        } elseif (365 <= $day) {
            return "1年前";
        } else {
            return $day . "天前";
        }
    } else {
        if ($free > 0) {
            $hour = floor($free / 3600);
            $free = $free % 3600;
            if ($hour > 0) {
                return $hour . "小时前";
            } else {
                if ($free > 0) {
                    $min = floor($free / 60);
                    $free = $free % 60;
                    if ($min > 0) {
                        return $min . "分钟前";
                    } else {
                        if ($free > 0) {
                            return $free . "秒前";
                        } else {
                            return '刚刚';
                        }
                    }
                } else {
                    return '刚刚';
                }
            }
        } else {
            return '刚刚';
        }
    }
}


function check_date($date) { //检查日期是否合法日期
    $dateArr = explode("-", $date);
    if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
        return checkdate($dateArr[1],$dateArr[2],$dateArr[0]);
    }
    return false;
}
function check_time($time) {  //检查时间是否合法时间
    $timeArr = explode(":", $time);
    if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
        if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
            return true;
        else
            return false;
    }
    return false;
}

//显示用户友好的时间
//一分钟内“刚刚”一小时内“* 分钟前”今天内“今天 小时：分钟”昨天内“昨天 小时：分钟“大于2天”年-月-日 小时：分钟“
//格式化时间
function format_date($beforetime)
{
    $time= time() - $beforetime;
    $today=strtotime(date("M-d-y",mktime(0,0,0,date("m"),date("d"),date("Y"))));
    if($time <= 60){
        return '刚刚';
    }elseif($time>=60 && $time<3600){
        $return=intval($time / 60)." 分钟前";
    }else{
        if($beforetime>$today){
            $return="今天 ".date("H:i",$beforetime);
        }elseif($beforetime<$today && $beforetime>($today-86400)){
            $return="昨天 ".date("H:i",$beforetime);
        }else{
            $return=date("Y-m-d H:i",$beforetime);
        }
    }
    return $return;
}



//返回一个时间段内所有月份  传时间戳

function monthList($start,$end){
    if(!is_numeric($start)||!is_numeric($end)||($end<=$start)) return '';
    $start=date('Y-m',$start);
    $end=date('Y-m',$end);
    //转为时间戳
    $start=strtotime($start.'-01');
    $end=strtotime($end.'-01');
    $i=0;
    $d=array();
    while($start<=$end){
        //这里累加每个月的的总秒数 计算公式：上一月1号的时间戳秒数减去当前月的时间戳秒数
        $d[$i]=trim(date('Y-m',$start),' ');
        $start+=strtotime('+1 month',$start)-$start;
        $i++;
    }
    return $d;

}



//返回一个月份的第一天和最后一天

function getthemonth($date)
{
    $firstday = date('Y-m-01', strtotime($date));
    $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
    return array($firstday,$lastday);
}
$today = date("Y-m-d");
$day=getthemonth($today);



//返回当前日期或者指定日期是星期几


//PHP星期几获取代码：
date("l"); //data就可以获取英文的星期比如Sunday
date("w"); //这个可以获取数字星期比如123，注意0是星期日
//获取中文星期几：
$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
echo "星期".$weekarray[date("w")];
//获取指定日期是：
$weekarray=array("日","一","二","三","四","五","六");
echo "星期".$weekarray[date("w","2011-11-11")];